package com.my.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.my.entity.User;
import com.my.entity.UserMenu;
import com.my.service.UserMenuService;
import com.my.service.UserService;
import com.my.util.Rest;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@CrossOrigin
public class TeacherController {

    @Resource
    private UserService service;

    @Resource
    private UserMenuService umService;

    private static final Long[] TEACHER_MENU = { 5L, 6L, 7L, 8L, 9L, 10L };

    @PostMapping("teacher/get")
    public Rest get(@RequestParam("pageNo") int pageNo, @RequestParam("pageSize") int pageSize, @RequestBody User user) {
        return Rest.ok().setData("page", service.page(new Page<>(pageNo, pageSize), new QueryWrapper<User>()
                .like(StrUtil.isNotBlank(user.getName()), "name", user.getName())
                .like(StrUtil.isNotBlank(user.getUsername()), "username", user.getUsername())
                .like(StrUtil.isNotBlank(user.getPhone()), "phone", user.getPhone())
                .eq("type", 2).select(User.class, i -> !i.getColumn().equals("password"))));
    }

    @Transactional(rollbackFor = Exception.class)
    @PostMapping("teachers")
    public Rest add(@RequestBody User user) {
        if(service.save(user.setPassword(SecureUtil.md5(StrUtil.isNotBlank(user.getPassword()) ? user.getPassword() : "123456")))) {
            for(Long i : TEACHER_MENU) {
                umService.save(new UserMenu().setUserId(user.getId()).setType(2).setMenuId(i));
            }
            return Rest.ok("添加成功！");
        }
        return Rest.err("添加失败！");
    }

    @PutMapping("teachers")
    public Rest edit(@RequestBody User user) {
        if(StrUtil.isBlank(user.getPassword())) {
            user.setPassword(null);
        }else {
            user.setPassword(SecureUtil.md5(user.getPassword()));
        }
        if(service.updateById(user)) {
            return Rest.ok("修改成功！");
        }
        return Rest.err("修改失败！");
    }

    @GetMapping("teacher/repeat")
    public Rest repeat(@RequestParam String username, @RequestParam(defaultValue = "-1") Long id) {
        QueryWrapper<User> qw = new QueryWrapper<User>().eq("username", username).eq("type", 2);
        if(id != -1) {
            qw.ne("id", id);
        }
        return Rest.ok().setData("flag", service.getOne(qw) != null);
    }

    @PostMapping("teacher/status")
    public Rest status(@RequestBody User user) {
        if(service.updateById(user)) {
            return Rest.ok("状态修改成功！");
        }
        return Rest.err("状态修改失败！");
    }

    @DeleteMapping("teachers")
    public Rest del(Long id) {
        if(service.removeById(id)) {
            umService.remove(new QueryWrapper<UserMenu>().eq("user_id", id).eq("type", 2));
            return Rest.ok("删除成功！");
        }
        return Rest.err("删除失败！");
    }

}
